<template>
  <div class="total-goods">
    <div>
      <div class="search-box">
        <el-date-picker
          size="small"
          v-model="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
        <el-button @click="onSubmit" style="margin-left:20px" size="small" type="primary">查询</el-button>
      </div>
    </div>
    <div class="order-echarts">
      <div class="my-echart" ref="chartRef"></div>
    </div>
  </div>
</template>

<script>
import { orderOrdertotal } from "@/api/order";
import { formatYMDhms } from '@/filters';
import * as echarts from "echarts";
export default {
  data() {
    return {
      date: [],
    };
  },
  methods: {
    async getData() {
      let res = await orderOrdertotal({
        date: JSON.stringify(this.date),
      });
      console.log(res);
      let {data} =res.data
      let xData=data.map(v=>formatYMDhms(v.orderTime))
      let series=[
        {
          type: "bar",
          name: "金额数据",
          data: data.map((v) => v.orderAmount),
        }
      ]
      this.renderLine({
        xData,
        series,
        title:'金额图表'
      })
    },
    //画折线图
     renderLine(obj) {
      //默认值
      let defaultOptions = {
        title: "默认标题",
      };
      //最终参数 传入参数合并默认值
      let finalObj = { ...defaultOptions, ...obj };

      // 基于准备好的dom，初始化echarts实例
      let myChart = echarts.init(this.$refs.chartRef);
      //配置项
      let options = {
        title: {
          text: finalObj.title,
          left: 15,
          top: 15,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          top: 15,
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: finalObj.xData,
        },
        yAxis: {
          type: "value",
        },
        //核心数据
        series: finalObj.series,
      };
      // 绘制图表
      myChart.setOption(options);
    },
    onSubmit() {
      this.getData()
    },
  },
  mounted(){
    this.getData()
  }
};
</script>

<style lang="scss" scoped>
.total-goods {
  padding: 20px;
}
.order-echarts {
  background-color: #fff;
  margin-top: 30px;
}
.my-echart {
  width: 100%;
  height: 400px;
  margin-top: 100px;
}
</style>